package com.example.sachon;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.example.sachon.ChonBoardNewListAdapter.ViewHolder;
import com.example.sachon.vo.ChonInfo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class ChonTileAdapter extends BaseAdapter {

	private Context context;
	private int viewResourceId;
	private LayoutInflater inflater;
	private ArrayList<ChonInfo> data;
	// references to our images

	public ChonTileAdapter(ArrayList<ChonInfo> data, int viewResourceId,  Context  context){
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.viewResourceId = viewResourceId;
		this.data = data;
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}
	/*
	public View getView(int position, View convertView, ViewGroup parent) {
		 
		LayoutInflater inflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View gridView;
 
	//	if (convertView == null) {
 
			gridView = new View(context);
 
			// get layout from mobile.xml
			gridView = inflater.inflate(R.layout.activity_chon_tile_context, null);
 
			// set value into textview
			TextView textView = (TextView) gridView
					.findViewById(R.id.txt_chon_name2);
			textView.setText(data.get(position).getChon_nm());
 
			// set image based on selected text
			ImageView imageView = (ImageView) gridView
					.findViewById(R.id.img_chon_img2);
			imageView.setAdjustViewBounds(true);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	        DownloadImageTask task = new DownloadImageTask(imageView);
            task.execute(Const.SERVER_IMAGE_URL+data.get(position).getChon_small_img()); 
//		} else {
//			gridView = (View) convertView;
//		}
 
		return gridView;
	}
	*/
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			
			convertView = inflater.inflate(R.layout.activity_chon_tile_context, null);
			
			viewHolder.textView = (TextView) convertView
					.findViewById(R.id.txt_chon_name2);
			viewHolder.imageView = (ImageView) convertView
						.findViewById(R.id.img_chon_img2);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		viewHolder.textView.setText(data.get(position).getChon_nm());
		try {
			String urlString =Const.SERVER_IMAGE_URL+data.get(position).getChon_small_img();
			System.out.println(urlString);
			
			ImageDownloader.download(urlString, viewHolder.imageView);
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return convertView;		
	}
	    
	public final class ViewHolder {
		private TextView textView;
		private ImageView imageView;
	}
}
